package com.cry.app.core;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Properties;
import java.util.Set;

import javax.servlet.ServletContext;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.context.support.ServletContextResource;

public class ContextPropertyPlaceholder extends PropertyPlaceholderConfigurer implements ServletContextAware {
	private ServletContext servletContext;
	private Resource[] locations;
	private Resource location;
	private HashMap<String, String> locationPaths = new HashMap<String, String>();


	@Override
	public void setLocations(Resource[] locations) {
		this.locations = locations;
	}
	
	@Override
	public void setLocation(Resource location) {
		this.location = location;
	}
	
	@Override
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
		Enumeration names = this.servletContext.getInitParameterNames();
		while (names.hasMoreElements()) {
		  String name =  (String) names.nextElement();
		  String value = this.servletContext.getInitParameter(name);
		  this.locationPaths.put(name, value);
		}		
		
	}

	@Override
	protected void loadProperties(Properties props) throws IOException {
		ArrayList<Resource> newLocations = new ArrayList<Resource>();
		
		if ( this.locations!= null) {
			for (Resource location : this.locations) {
				String path = "";
				if(location instanceof ServletContextResource) {
					path = ((ServletContextResource) location).getPath();
				}
				if(location instanceof ClassPathResource) {
					path = ((ClassPathResource) location).getPath();
				}
				/*
				 * Do the replace
				 */
				for(String key :this.locationPaths.keySet()) {
					path = path.replace("$(" + key + ")", this.locationPaths.get(key));
				}

				if(location instanceof ServletContextResource) {
					newLocations.add(new ServletContextResource(servletContext, path));
				}
				if(location instanceof ClassPathResource) {
					newLocations.add(new ClassPathResource(path));
				}
			}
		}
		
		super.setLocations(newLocations.toArray( new Resource[newLocations.size()] ));
		super.loadProperties(props);
	}
	
	@Override
	protected String parseStringValue(String strVal, Properties props,
			Set visitedPlaceholders) {
		return super.parseStringValue(strVal, props, visitedPlaceholders);
	}
	
	@Override
	protected String resolvePlaceholder(String placeholder, Properties props) {
		return super.resolvePlaceholder(placeholder, props);
	}
	@Override
	protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
		super.processProperties(beanFactoryToProcess, props);
	}
	
	@Override
	protected String convertPropertyValue(String originalValue) {
		return super.convertPropertyValue(originalValue);
	}
	
}
